package com.xiaopang.fitment.Controller.User;

import com.xiaopang.fitment.Pojo.R;
import com.xiaopang.fitment.Pojo.domain.User;
import com.xiaopang.fitment.Service.User.UserService;
import com.xiaopang.fitment.Utils.EmailUtil;
import com.xiaopang.fitment.Utils.SmsUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/user")
public class UserController {

    private final EmailUtil emailUtil;


    public UserController(EmailUtil emailUtil) {
        this.emailUtil = emailUtil;
    }

    @Autowired
    private UserService userService;

    @Operation(summary = "Say Hello", description = "Returns a greeting message to the user")
    @GetMapping("/hello/{userId}")
    public String hello(
            @Parameter(description = "ID of the user to greet", required = true)
            @PathVariable String userId) {
        return "Hello, " + userId + "!";
    }

    @Operation(summary = "采用手机号进行登录")
    @PostMapping("/loginByPhone")
    public R loginByPhone(@RequestBody User user) {
        String Psw = userService.FindPsw(user.getPhone());
        if (Psw == null) {
            return R.fail();
        } else if (Psw.equals(user.getPassword())) {
            return R.success("成功登录");
        } else {
            return R.fail();
        }
    }

    @Operation(summary = "手机发送验证码")
    @GetMapping("phoneSendCaptcha")
    public R phoneSendCaptcha(String phone) {
        SmsUtil smsUtil = new SmsUtil();
        smsUtil.tosend();
        return R.success();
    }

    @Operation(summary = "邮箱发送验证码")
    @GetMapping("emailSendCaptcha")
    public R emailSendCaptcha(String email) {
        emailUtil.sendEmailCode(email);

        return R.success();
    }

    @Operation(summary = "采用手机验证码进行登录")
    @PostMapping("/loginByCaptcha")
    public R loginByCaptcha(@RequestBody User user) {
        return R.success();
    }

}
